package com.maxatime.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.maxatime.pojo.dto.EmployeeInfoDTO;
import com.maxatime.pojo.entity.Areas;
import com.maxatime.pojo.entity.Cities;
import com.maxatime.pojo.entity.Provinces;
import com.maxatime.service.EmployeelnfoService;
import com.maxatime.service.ProvincesS;
import com.maxatime.util.MyContent;

@Controller
@RequestMapping("/yuangong")
public class Employeelnfocontroller {
	//员工管理 
		// 日志
		Logger log = LogManager.getLogger();
		
		@Autowired
		EmployeelnfoService employeelnfoService;
		
		@Autowired
		ProvincesS ProvincesS;//三级联动
		
		/**
		 * 
		 * @Title: yuangongmainpage   
		 * @Description: TODO：员工管理系统跳转html
		 * @param: @return      
		 * @return: String      
		 * @throws   
		 * @author: ssb    
		 * @date:   2023年10月30日 下午2:04:29   
		 * @version V1.0
		 */
		@RequestMapping("/yuangongmainpage")
		public String yuangongmainpage() {
			log.info("mainpage-begin");
			return "yuangong/mainpage";
		}
		/**
		 * 
		 * @Title: queryData   
		 * @Description: TODO员工管理系统查询  
		 * @param: @param jsonStr
		 * @param: @return      
		 * @return: Map<String,Object>      
		 * @throws   
		 * @author: ssb    
		 * @date:   2023年11月2日 下午2:59:25   
		 * @version V1.0
		 */
		
		@RequestMapping("/queryData")
		@ResponseBody
		public Map<String,Object> queryData(@RequestBody String jsonStr){
			Map<String,Object> m = new HashMap<String,Object>();
			JSONObject jsonObj = JSONObject.parseObject(jsonStr);
			String phoneNum = jsonObj.getString("phoneNum");
			String departmentEmployees = jsonObj.getString("departmentEmployees");
			String employeeName = jsonObj.getString("employeeName");
			EmployeeInfoDTO employeeInfoDTO = new EmployeeInfoDTO();
			employeeInfoDTO.setPhoneNum(phoneNum);
			employeeInfoDTO.setEmployeeName(employeeName);
			employeeInfoDTO.setDepartmentEmployees(departmentEmployees);
			List<EmployeeInfoDTO> listDTO = employeelnfoService.listData(employeeInfoDTO);
			m.put(MyContent.CODE, "0000");
			m.put(MyContent.MSG, "交易成功");
			m.put(MyContent.DATA, listDTO);
			return m;
			
		}
		//新增
		@RequestMapping("/addData")
		@ResponseBody
		public Map<String,Object> addData(@RequestBody String jsonStr){
			JSONObject jsonObj = JSONObject.parseObject(jsonStr);
//			String userId = jsonObj.getString("userId");
			String employeeName = jsonObj.getString("employeeName");
			String age = jsonObj.getString("age");
			String email = jsonObj.getString("email");
			String phonenum = jsonObj.getString("phonenum");//手机号
			
			String homeProvinceid = jsonObj.getString("homeProvinceid");//家庭地址省id
			String homeProvince = jsonObj.getString("homeProvince");//家庭地址省名称
			String homeCityid = jsonObj.getString("homeCityid");//家庭地址市id
			String homeCity = jsonObj.getString("homeCity");//家庭地址市名称
			String homeAreaid = jsonObj.getString("homeAreaid");//家庭地址区县id
			String homeArea = jsonObj.getString("homeArea");//家庭地址区县名称
			
			
			Map<String,Object> m = new HashMap<String,Object>();
			EmployeeInfoDTO employeeInfoDTO = new EmployeeInfoDTO();
//			employeeInfoDTO.setUserId(userId);
			employeeInfoDTO.setEmployeeName(employeeName);
			employeeInfoDTO.setAge(Integer.parseInt(age));
			employeeInfoDTO.setEmail(email);
			employeeInfoDTO.setPhoneNum(phonenum);
			
			employeeInfoDTO.setHomeProvinceid(homeProvinceid);
			employeeInfoDTO.setHomeProvince(homeProvince);
			employeeInfoDTO.setHomeCityid(homeCityid);
			employeeInfoDTO.setHomeCity(homeCity);
			employeeInfoDTO.setHomeAreaid(homeAreaid);
			employeeInfoDTO.setHomeArea(homeArea);
			
			employeelnfoService.addData(employeeInfoDTO);
			m.put(MyContent.CODE, "0000");
			m.put(MyContent.MSG, "交易成功");

			return m;
			
		}
		//反显userID
		@RequestMapping("/findByuserId")
		@ResponseBody
		public Map<String,Object> findByuserId(@RequestBody String jsonStr){
			JSONObject jsonObj = JSONObject.parseObject(jsonStr);
			String userId = jsonObj.getString("userId");
			Map<String,Object> m = new HashMap<String,Object>();
			EmployeeInfoDTO employeeInfoDTO = employeelnfoService.selectByUserId(userId);
			m.put(MyContent.CODE, "0000");
			m.put(MyContent.MSG, "交易成功");
			m.put(MyContent.DATA, employeeInfoDTO);
			return m;
			
		}
		//删除
		@RequestMapping("/deleteData")
		@ResponseBody
		public Map<String,Object> deleteData(@RequestBody String jsonStr){
			JSONObject jsonObj = JSONObject.parseObject(jsonStr);
			String pkId = jsonObj.getString("pkId");//获取pkId
			Map<String,Object> m =new HashMap<String,Object>();
			int iRet = employeelnfoService.deleteByPrimaryKey(Integer.parseInt(pkId));
			m.put(MyContent.CODE, "0000");
			m.put(MyContent.MSG,"交易成功");
			return m;
			
		}
		//修改
		@RequestMapping("/updateData")
		@ResponseBody
		public Map<String,Object> updateData(@RequestBody String jsonStr){
			JSONObject jsonObj = JSONObject.parseObject(jsonStr);
			String userId = jsonObj.getString("userId");
			String employeeName = jsonObj.getString("employeeName");
			String age = jsonObj.getString("age");
			String email = jsonObj.getString("email");
			String phonenum = jsonObj.getString("phonenum");//手机号
			String homeProvince = jsonObj.getString("homeProvince");//家庭地址省名称
			String homeCity = jsonObj.getString("homeCity");//家庭地址市名称
			String homeArea = jsonObj.getString("homeArea");//家庭地址区县名称
			String pkId = jsonObj.getString("pkId");//获取pkId
			Map<String,Object> m = new HashMap<String,Object>();
			EmployeeInfoDTO employeeInfoDTO = new EmployeeInfoDTO();
			employeeInfoDTO.setUserId(userId);
			employeeInfoDTO.setEmployeeName(employeeName);
			employeeInfoDTO.setAge(Integer.parseInt(age));
			employeeInfoDTO.setEmail(email);
			employeeInfoDTO.setPhoneNum(phonenum);
			employeeInfoDTO.setHomeProvince(homeProvince);
			employeeInfoDTO.setHomeCity(homeCity);
			employeeInfoDTO.setHomeArea(homeArea);
			employeeInfoDTO.setPkId(Integer.parseInt(pkId));
			int iRet = employeelnfoService.updateByPrimaryKeySelective(employeeInfoDTO);
			m.put(MyContent.CODE, "0000");
			m.put(MyContent.MSG, "交易成功");
			m.put(MyContent.DATA, employeeInfoDTO);
			return m;
			
		}
		
		///三级联动
		//省
		@RequestMapping("/findProvinces")
		@ResponseBody
		public Map<String,Object> findProvinces(@RequestBody  Provinces provinces){
			Map<String, Object> m = new HashMap<String, Object>();
			List<Provinces> list=ProvincesS.selectProvinces(provinces);
			m.put("data", list);
			m.put("code", "0000");
			m.put("msg", "success");
			return m;
		}
		
		//市
		@RequestMapping("/findCities")
		@ResponseBody
		public Map<String,Object> findCities(@RequestBody String val){
			JSONObject jsonObj=JSONObject.parseObject(val);
			String provinceid=jsonObj.getString("provinceId");
			
			Map<String,Object> m=new HashMap<String,Object>();
			Cities Ci=new Cities();
			
			
			Ci.setProvinceid(provinceid);
			List<Cities> list=ProvincesS.selectCities(Ci);
			m.put("data", list);
			m.put("code", "0000");
			m.put("msg", "success");
			return m;
		}
		
		//县
		@RequestMapping("/findAreas")
		@ResponseBody
		public Map<String,Object> findAreas(@RequestBody String val){
			JSONObject jsonObj=JSONObject.parseObject(val);
			String cityid=jsonObj.getString("cityid");
			Map<String,Object> m=new HashMap<String,Object>();
			Areas Areas=new Areas();
			Areas.setCityid(cityid);
			List<Areas> list=ProvincesS.selectAreas(Areas);
			
			m.put("data", list);
			m.put("code", "0000");
			m.put("msg", "success");
			return m;
		}
}
